# -*- coding: utf-8 -*-

''' Autor: Neuza Figueira - n.º 6036 '''

from random import uniform
from time import clock
import math
import matplotlib.pyplot as plt

class CombSort():
        ''' Class to handle Comb sort implementation and testing.'''
        
        def comb_sort(self, A):
                '''Python Implementation of Comb sort.'''
                '''@param A -> List to sort.'''
                n = len(A)
                swapped = True
                
                while n > 1 or swapped == True:
                        n = max(1, int(n / 1.25))
                        swapped = False
                        
                        for i in range(len(A) - n):
                                j = i + n
                                
                                if A[i] > A[j]:
                                        A[i], A[j] = A[j], A[i]
                                        swapped = True

                return A

        
        def testCombSort(self):
                '''Testing the complexity of Comb sort.'''
                Z = range(50, 1050, 50)
                T = []
                M = 50
                for n in Z:
                    A = [ uniform(0.0, 1.0) for k in xrange(n)]
                    tempos = []
                    for k in range(M):
                        t1 = clock()
                        self.comb_sort(A)
                        t2 = clock()
                        tempos.append(t2-t1)
                    media = reduce(lambda x, y: x + y, tempos) / len(tempos)
                    var = reduce(lambda x, y: x + (y-media)**2, [0] + tempos) / len(tempos)
                    
                    T.append((n,media, var))

                X = [n for n, media, var in T]
                Y = [media for n, media, var in T]
                ct = 13e5
                Z = [(n* math.log(n)) / ct for n, media, var in T]


                plt.grid(True)
                plt.ylabel(u'T(n) - tempo de execução médio em segundos')
                plt.xlabel(u'n - número de elementos')
                plt.plot(X,Y,'rs', label="resultado experimental")
                plt.plot(X, Z, 'b^', label=u"previsão teórica")
                plt.legend()
                plt.show()
                pass
        
